<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="表单名称" prop="formName">
        <el-input v-model="dataForm.formName" placeholder="表单名称"></el-input>
      </el-form-item>
      <el-form-item label="表单说明" prop="formSm">
        <el-input v-model="dataForm.formSm" placeholder="表单说明" type="textarea" :rows="3"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/admin/sysformconf'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          formId: 0,
          formName: '',
          formSm: '',
          formInfo: '',
          createTime: '',
          editTime: '',
          createDeptId: '',
          createDeptName: '',
          createUserId: '',
          createUserName: '',
          editDeptId: '',
          editDeptName: '',
          editUserId: '',
          editUserName: '',
          dataState: '',
          tenantId: ''
        },
        dataRule: {
          formName: [
            { required: true, message: '表单名称不能为空', trigger: 'blur' }
          ]
          // ,formSm: [
          //   { required: true, message: '表单说明不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.formId = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.formId) {
            getObj(this.dataForm.formId).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.formId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.closeAll();
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.closeAll();
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
